package com.controller;

import com.model.Author;
import com.service.AuthorService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class AuthorServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("method");
        if (method == null || "list".equals(method)) {
            AuthorService authorService = new AuthorService();
            List<Author> authors = authorService.listAuthors();
            req.setAttribute("authors", authors);
            req.getRequestDispatcher("/Author.jsp").forward(req, resp);
            return;
        }
        if ("add".equals(method)) {
            String name = req.getParameter("name");
            String email = req.getParameter("email");

            AuthorService authorService = new AuthorService();
            boolean flag = authorService.addAuthor(new Author(name, email));
            if (flag) {
                req.setAttribute("message", "添加信息成功");
            } else {
                req.setAttribute("message", "添加信息失败！！！！");
            }
            req.setAttribute("requestURI", req.getContextPath() + "/Author");
            req.getRequestDispatcher("/info.jsp").forward(req, resp);
            return;
        }
        if ("delete".equals(method)) {
            int id = Integer.parseInt(req.getParameter("id"));

            AuthorService authorService = new AuthorService();
            boolean flag = authorService.deleteAuthorById(id);
            if (flag) {
                req.setAttribute("message", "删除信息成功");
            } else {
                req.setAttribute("message", "删除信息失败！！！！");
            }
            req.setAttribute("requestURI", req.getContextPath() + "/Author");
            req.getRequestDispatcher("/info.jsp").forward(req, resp);
            return;
        }
        if ("update".equals(method)) {
            int id = Integer.parseInt(req.getParameter("id"));
            String name = req.getParameter("name");
            String email = req.getParameter("email");

            AuthorService authorService = new AuthorService();
            boolean flag = authorService.updateAuthor(new Author(id, name, email));
            if (flag) {
                req.setAttribute("message", "更新信息成功");
            } else {
                req.setAttribute("message", "更新信息失败！！！！");
            }
            req.setAttribute("requestURI", req.getContextPath() + "/Author");
            req.getRequestDispatcher("/info.jsp").forward(req, resp);
            return;
        }
        if ("search".equals(method)) {
            String name = req.getParameter("name");
            System.out.println(name);
            if ("".equals(name)){
                resp.sendRedirect(req.getContextPath()+"/Author");
                return;
            }
            AuthorService authorService = new AuthorService();
            List<Author> authors = authorService.searchAuthorByName(name);
            req.setAttribute("authors", authors);
            req.getRequestDispatcher("/Author.jsp").forward(req, resp);
            return;
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
